//
//  SAWAssignHomeworkVC.m
//  优学荟管理系统
//
//  Created by edz on 2018/1/26.
//  Copyright © 2018年 星道三好. All rights reserved.
//
#import "SAWHomeWorkWebVC.h"
#import "SAWAssignModel.h"
#import "SAWAssignHomeworkVC.h"
#import "SAWSetHomeworkTabCell.h"
@interface SAWAssignHomeworkVC ()<UITableViewDelegate,UITableViewDataSource>
@property(nonatomic,assign)NSInteger page;
@property (strong, nonatomic)NSArray *XQArr;
@property (strong, nonatomic)UITableView *tableView;
@property (strong, nonatomic)SAWAssignModel *model;
@property (strong, nonatomic)UIButton *comitBtn;
@property(nonatomic,assign)NSInteger countItem;
@end

@implementation SAWAssignHomeworkVC
- (void)itemAction{
    [self.navigationController popViewControllerAnimated:YES];
}
- (void)viewDidLoad {
    [super viewDidLoad];
    self.navigationItem.leftBarButtonItem = [UIBarButtonItem itemWithTargat:self action:@selector(itemAction) image:@"icon_back_student_sign" highImage:@"icon_back_student_sign"];
   
     self.navigationItem.title=@"布置作业";
    _page = 1;
  
    [self.model getUserXQ:@[self.uniCode,[NSString stringWithFormat:@"%ld",self.page]] withData:^(NSArray *array) {
        self.XQArr = array;
        [_tableView reloadData];
    }];
    NSLog(@"-------%@------%@",self.homeworkID,self.uniCode);
    [self setUI];
}
-(SAWAssignModel *)model
{
    if (!_model) {
        _model = [[SAWAssignModel alloc]init];
    }
    
    return _model;
}
-(NSArray *)XQArr
{
    if (!_XQArr) {
        _XQArr = [[NSArray alloc]init];
    }
    
    return _XQArr;
}
-(void)setUI
{
   _tableView = [[UITableView alloc]initWithFrame:CGRectMake(0, 0, kScreenWidth, kScreenHeight-kTopHeight)];
    [self.view addSubview:_tableView];
    _tableView.backgroundColor=RGBColor(245, 245, 245);
    _tableView.separatorStyle=UITableViewCellSeparatorStyleNone;
    [_tableView registerClass:[SAWSetHomeworkTabCell class] forCellReuseIdentifier:@"Cell"];
    _tableView.rowHeight=80;
    _tableView.delegate=self;
    _tableView.dataSource=self;
    
    _comitBtn = [[UIButton alloc]init];
    [self.view addSubview:_comitBtn];
    [_comitBtn setTitle:[NSString stringWithFormat:@"已选0道题,点击预览"] forState:0];
    [_comitBtn setBackgroundImage:[UIImage imageNamed:@"btn_ensure_default_See_Teacher"] forState:0];
    [_comitBtn addTarget:self action:@selector(saveData) forControlEvents:UIControlEventTouchUpInside];
    [_comitBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.offset(15);
        make.right.offset(-15);
        make.bottom.offset(-15);
        make.height.offset(40);
    }];
   
}
-(void)saveData
{
    if (self.countItem==0) {
        [SVProgressHUD showErrorWithStatus:@"请先选择作业题"];
    }
    else
    {
        SAWHomeWorkWebVC *vc = [[SAWHomeWorkWebVC alloc]init];
        vc.homeworkID = self.homeworkID;
        
        [self.navigationController pushViewController:vc animated:YES];
    }
   
    
}
- (void)viewWillAppear:(BOOL)animated {
    
    [super viewWillAppear:animated];
    [self.navigationController.navigationBar setBackgroundImage:[LLTools createImageWithColor:ColorRGB(255, 255, 255)] forBarMetrics:(UIBarMetricsDefault)];//设置导航栏颜色
    [self.navigationController.navigationBar setTitleTextAttributes:@{NSForegroundColorAttributeName:ColorRGB(51,51,51)}];//设置标题颜色
}

-(void)viewWillDisappear:(BOOL)animated {
    [super viewWillDisappear:animated];
    
    [self.navigationController.navigationBar setTitleTextAttributes:@{NSForegroundColorAttributeName:ColorRGB(255,255,255)}]; //设置标题颜色
    [self.navigationController.navigationBar setBackgroundImage:[LLTools createImageWithColor:ColorRGB(47, 204, 138)] forBarMetrics:(UIBarMetricsDefault)];//设置导航栏颜色
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.XQArr.count;
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    SAWSetHomeworkTabCell *cell = [tableView dequeueReusableCellWithIdentifier:@"Cell" forIndexPath:indexPath];
    NSDictionary *dic = self.XQArr[indexPath.row];
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    cell.titLab.text=dic[@"knowledgeName"];
    
    cell.selBtn.tag = 10001+indexPath.row;
    [cell.selBtn addTarget:self action:@selector(selItem:) forControlEvents:UIControlEventTouchUpInside];
    return cell;
}
-(void)selItem:(UIButton *)sender
{
    
    NSDictionary *dic = self.XQArr[sender.tag-10001];
    
    [self.model getItemData:@[self.homeworkID,[NSString stringWithFormat:@"%@",dic[@"id"]]] withData:^() {
        sender.selected=YES;
        [sender setTitle:[NSString stringWithFormat:@"已选10道"] forState:0];
        self.countItem =10 + self.countItem;
        [_comitBtn setTitle:[NSString stringWithFormat:@"%@", [NSString stringWithFormat:@"已选%ld道题，点击预览",self.countItem]] forState:0];
    }];
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
